import java.util.ArrayList;
import java.util.Objects;
import java.util.Stack;

/**
 * 判断链表是否是回文结构。
 * 借助栈的特性，先进后出。
 */
public class JavaDemo13 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 2, 1, 0};
        ListNode head = new ListNode(0);
        ListNode next = head;
        for (int i = 0; i < arrays.length; i++) {
            ListNode node = new ListNode(arrays[i]);
            next.next = node;
            next = node;
        }
        ListNode showParent = head;
        while (showParent != null) {
            System.out.println(showParent.val);
            showParent = showParent.next;
        }
        System.out.println("--------------" + palindromic(head));
    }

    private static boolean palindromic(ListNode head) {
        ListNode old=head;
        Stack<Integer> nodes = new Stack<>();
        while (head != null) {
            nodes.push(head.val);
            head = head.next;
        }
        while (!nodes.isEmpty()){
            if (nodes.pop()!=old.val){
                return false;
            }else {
                old=old.next;
            }
        }
        return true;
    }


}
